﻿using System;
using System.Collections;
using mojoPortal.Business;
using mojoPortal.Web.Framework;
using mojoPortal.Web;
using Resources;

namespace ud_SwirlingQuestion
{
    public partial class SearchResultPage : mojoBasePage
    {
        protected int pageId = -1;
        protected int moduleId = -1;
        private string searchTerm = string.Empty;
        private bool userCanEdit;
        private int pageNumber = 1;
        private SwirlingQuestionConfiguration config = new SwirlingQuestionConfiguration();

        #region OnInit
        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
        }

        override protected void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            Load += Page_Load;
            base.OnInit(e);
        }
        #endregion

        private void LoadPanels()
        {
            LoadSideContent(config.ShowLeftPanelSetting, config.ShowRightPanelSetting, config.ShowTopPanelSetting, config.ShowBottomPanelSetting, config.ShowNumberModuleSetting);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!UserCanViewPage(moduleId))
            {
                SiteUtils.RedirectToAccessDeniedPage();
                return;
            }

            LoadSettings();
            PopulateControls();

        }

        private void PopulateControls()
        {
            Title = SiteUtils.FormatPageTitle(siteSettings, SwirlingQuestionResource.SwirlingQuestionFeatureName);
            moduleTitle.ModuleInstance = GetModule(moduleId);
            SearchPlace.Config = config;
            SearchPlace.PageId = pageId;
            SearchPlace.ModuleId = moduleId;
            SearchPlace.SiteRoot = SiteRoot;
            qaList.ModuleId = moduleId;
            qaList.PageId = pageId;
            qaList.IsEditable = userCanEdit;
            qaList.SiteRoot = SiteRoot;
            qaList.ImageSiteRoot = ImageSiteRoot;
            qaList.Config = config;
            qaList.SearchTerm = searchTerm;
            qaList.PageNumber = pageNumber;
            //make this page look as close as possible to the way a cms page with the blog module on it looks
        }

        private void LoadSettings()
        {
            userCanEdit = UserCanEditModule(moduleId);
            pnlWrapper.CssClass += " " + config.ResultCss;
            //pnl1.CssClass += " " + config.InstanceCssClass;
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", moduleId);
            pageNumber = WebUtils.ParseInt32FromQueryString("pagenumber", pageNumber);
            searchTerm = Session["qasearchterm"] == null ? string.Empty : Session["qasearchterm"].ToString();
            Session["qasearchterm"] = searchTerm;
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            config = new SwirlingQuestionConfiguration(settings);
        }
    }
}